<template>
    <div class="app">
        <div>
            <el-card class="box-card">
                <div slot="header" class="clearfix">
                    <span>商品添加</span>
                    <el-button style="float: right; padding: 3px 0" type="text">操作按钮</el-button>
                </div>
                <div class="text item">
                    <div class="order_box">
                        <div class="line">
                            商品名称
                            <el-input v-model="formData.name" placeholder="请输入内容"></el-input>
                        </div>
                        <div class="line">
                            商品分类
                            <el-select v-model="formData.category" placeholder="请选择">
                                <el-option v-for="item in formData.options" :key="item.category" :label="item.label"
                                    :value="item.value">
                                </el-option>
                            </el-select>
                        </div>
                        <div class="line">
                            商品价格
                            <el-input-number v-model="formData.price" @change="handleChange" :min="1" :max="10"
                                label="描述文字"></el-input-number>
                        </div>
                        <div class="line">
                            商品图片
                            <!-- <el-upload class="avatar-uploader" action="https://jsonplaceholder.typicode.com/posts/"
                                :show-file-list="false" :on-success="handleAvatarSuccess"
                                :before-upload="beforeAvatarUpload">
                                <img v-if="imageUrl" :src="imageUrl" class="avatar">
                                <i v-else class="el-icon-plus avatar-uploader-icon"></i>
                            </el-upload> -->
                        </div>
                        <div class="line">
                            商品描述
                            <el-input type="textarea" :rows="2" placeholder="请输入内容" v-model="formData.goodsDesc">
                            </el-input>
                        </div>
                        <el-button type="primary">主要按钮</el-button>
                    </div>
                </div>
            </el-card>
        </div>
    </div>
</template>

<script>
import { addproduct } from '@/api/user';
export default {
    data() {
        return {
            formData: {
                goodsDesc: '',
                imageUrl: '',
                name: '',
                price: 1,
                options: [{
                    category: '选项1',
                    label: '黄金糕'
                }, {
                    category: '选项2',
                    label: '双皮奶'
                }, {
                    category: '选项3',
                    label: '蚵仔煎'
                }, {
                    category: '选项4',
                    label: '龙须面'
                }, {
                    category: '选项5',
                    label: '北京烤鸭'
                }],
                value: '',
            },
        }
    },
    methods: {
        submit() {
            this.$refs.formRef.validate(async valid => {
                
                    //发送请求
                    let res = await addproduct(this.formData)
                    //解构数据
                    let { code, msg } = res.data
                    //判断
                    if (code === 0) {
                        //跳转
                        this.$router.push('/account')
                  
                }
            })
        },
        handleChange(value) {
            // console.log(value);
        },
        handleAvatarSuccess(res, file) {
            this.imageUrl = URL.createObjectURL(file.raw);
        },
        beforeAvatarUpload(file) {
            const isJPG = file.type === 'image/jpeg';
            const isLt2M = file.size / 1024 / 1024 < 2;

            if (!isJPG) {
                this.$message.error('上传头像图片只能是 JPG 格式!');
            }
            if (!isLt2M) {
                this.$message.error('上传头像图片大小不能超过 2MB!');
            }
            return isJPG && isLt2M;
        }
    }
}

</script>
<style lang="scss" scoped>
.app {
    margin: 20px 0px 0px 20px;

    .order_box {
        padding: 20px 0px 0px 20px;


        .line {
            margin-bottom: 20px;

            .el-input {
                width: 500px;
            }

            .avatar-uploader .el-upload {
                border: 1px dashed #d9d9d9;
                border-radius: 6px;
                cursor: pointer;
                position: relative;
                overflow: hidden;
            }

            .avatar-uploader .el-upload:hover {
                border-color: #409EFF;
            }

            .avatar-uploader-icon {
                font-size: 28px;
                color: #8c939d;
                width: 178px;
                height: 178px;
                line-height: 178px;
                text-align: center;
            }

            .avatar {
                width: 178px;
                height: 178px;
                display: block;
            }

            .el-icon-plus {
                border: 1px dashed rgb(215, 215, 215);
                margin-left: 70px;
            }

            .el-textarea {
                width: 500px;
            }

            .el-button {
                margin-top: 20px;
            }
        }

        :deep .el-input-number__decrease,
        :deep .el-input-number__increase {
            height: 38px;
            line-height: 38px;
        }
    }
}
</style>